#include <myhead.h>
uint32_t getip(void);
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;      
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
	int servr = socket(AF_INET,SOCK_STREAM,0);
	//准备一个tcp用的结构体
	addr_in_t addr={0};
	addr.sin_family=AF_INET;
	addr.sin_port=htons(12345);
	addr.sin_addr.s_addr=getip();
	//发起请求
	if(connect(servr,(addr_t*)&addr,sizeof(addr))==-1)
	{
		printf("连接失败\n");
		perror("bind");
		return 1;
	}
	printf("连接成功\n");
	while(1)
	{
		char buf[128]={0};
		printf("请输入参数:");
		scanf("%127s",buf);
		while(getchar()!=10);
		write(servr,buf,strlen(buf));
	}
	return 0;
}
uint32_t getip(void)
{
    struct ifaddrs *ifAddrStruct = NULL;
	struct ifaddrs *ifa = NULL;
	void *tmpAddrPtr = NULL;

	getifaddrs(&ifAddrStruct);

	for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) 
	{
		if (ifa ->ifa_addr->sa_family == AF_INET) 
		{ 
			tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
			char addressBuffer[INET_ADDRSTRLEN];
			inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
			if (strcmp(ifa->ifa_name, "lo") != 0)
			{ 
				printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
                		return ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
            		}
		}
	}
	if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
}